local skel = fk.CreateSkill {
  name = "emo__ouxiang",
}

Fk:loadTranslationTable{
  ["emo__ouxiang"] = "偶像",
  [":emo__ouxiang"] = "每牌名限一次，你可以将至多四张花色均不同且点数和为13的倍数的牌当任意基本牌或普通锦囊牌使用或打出。此牌结算后，你执行第X项："..
  "<br>1.获得X张花色与你以此法转化牌相同的牌。"..
  "<br>2.获得X张点数等于你以此法转化牌中最大点数的牌。"..
  "<br>3.获得X张点数相同且与你以此法转化牌的花色对应相同的牌。"..
  "<br>4.获得X张花色相同且与你以此法转化牌的点数对应相同的牌。"..
  "<br>若没有符合的牌，你获得X张【影】（X为以此法转化的牌数）。",

  ["#emo__ouxiang-prompt"] = "偶像：选择花色均不同且点数和为13倍数的牌（当前点数：%arg，还差%arg2点！）",
  ["@$emo__ouxiang_card"] = "偶像",
}

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|trick,basic",
  prompt = function (self, player, selected_cards, selected)
    -- 计算总数和距离下个13的数
    local count, rest = 0, 13
    for _, cid in ipairs(selected_cards) do
      count = count + Fk:getCardById(cid).number
    end
    if count > 0 then
      rest = math.ceil(count / 13) * 13 - count
    end
    return "#emo__ouxiang-prompt:::"..count..":"..rest
  end,
  interaction = function (self, player)
    local used = player:getTableMark("@$emo__ouxiang_card")
    local all_names = table.filter(Fk:getAllCardNames("bt"), function (name)
      return not table.contains(used, name)
    end)
    local names = player:getViewAsCardNames(self.name, all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    if #selected >= 4 then return false end
    local card = Fk:getCardById(to_select)
    if card.number == 0 or card.suit == Card.NoSuit then return false end
    return not table.find(selected, function(id) return Fk:getCardById(id).suit == card.suit end)
  end,
  view_as = function (self, player, cards)
    if #cards == 0 or #cards > 4 then return end
    if Fk.all_card_types[self.interaction.data] == nil then return end
    local n = 0
    for _, id in ipairs(cards) do
      n = n + Fk:getCardById(id).number
    end
    if n % 13 == 0 then
      local c = Fk:cloneCard(self.interaction.data)
      c.skillName = self.name
      c:addSubcards(cards)
      return c
    end
  end,
  before_use = function(self, player, use)
    -- 记录name，不同种杀可以多次印
    player.room:addTableMark(player, "@$emo__ouxiang_card", use.card.name)
  end,
  after_use = function (self, player, use) -- 打出牌也能生效哦
    if player.dead then return end
    local room = player.room
    local subcards = use.card.subcards
    local num = #subcards
    local ids = {}
    if num == 1 then
      local c = Fk:getCardById(subcards[1])
      ids = room:getCardsFromPileByRule(".|.|"..c:getSuitString(), 1)
    elseif num == 2 then
      local n = 0
      for _, id in ipairs(subcards) do
        n = math.max(n, Fk:getCardById(id).number)
      end
      ids = room:getCardsFromPileByRule(".|"..n, 2)
    elseif num == 3 then
      local list = {}
      local map = {}
      for _, id in ipairs(subcards) do
        table.insert(list, Fk:getCardById(id).suit)
      end
      for i = 1, 13 do
        map[i] = {}
      end
      for _, id in ipairs(room.draw_pile) do
        local c = Fk:getCardById(id)
        if map[c.number] then
          if #table.filter(list, function(t) return t == c.suit end) > #table.filter(map[c.number], function(t) return t[1] == c.suit end) then
            table.insert(map[c.number], {c.suit, id})
            if #map[c.number] == 3 then
              ids = table.map(map[c.number], function(t) return t[2] end)
              break
            end
          end
        end
      end
    elseif num == 4 then
      local list = {}
      local map = {}
      for _, id in ipairs(subcards) do
        table.insert(list, Fk:getCardById(id).number)
      end
      for i = 1, 4 do
        map[i] = {}
      end
      for _, id in ipairs(room.draw_pile) do
        local c = Fk:getCardById(id)
        if map[c.suit] then
          if #table.filter(list, function(t) return t == c.number end) > #table.filter(map[c.suit], function(t) return t[1] == c.number end) then
            table.insert(map[c.suit], {c.number, id})
            if #map[c.suit] == 4 then
              ids = table.map(map[c.suit], function(t) return t[2] end)
              break
            end
          end
        end
      end
    end
    if #ids == 0 then
      for _ = 1, num do
        local card = room:printCard("shade", Card.Spade, 1)
        room:setCardMark(card, MarkEnum.DestructIntoDiscard, 1)
        table.insert(ids, card.id)
      end
    end
    if #ids > 0 then
      room:moveCardTo(ids, Player.Hand, player, fk.ReasonPrey, self.name)
    end
  end,
  enabled_at_play = function(self, player)
    return not player:isNude()
  end,
  enabled_at_response = function (self, player, response)
    if not player:isNude() then
      local mark = player:getTableMark("@$emo__ouxiang_card")
      local all_names = Fk:getAllCardNames("bt")
      return table.find(player:getViewAsCardNames(self.name, all_names), function (name)
        return not table.contains(mark, name)
      end)
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$emo__ouxiang_card", 0)
end)

return skel
